function init(){
    imgPreview();
    if($('.item-list').length > 0){
        showPanelUpdate('.item-list', getUpdatePostRow);
        submitUpdate(updatePostRow);
        changePostState();
    } else {
        submitUpdateForm('#btn-submit-post', submitPost);
        setPostImage();
        uploadImage($('#frm-upload-img'), getUploadedImage);
        doCropImage(evtAfterCropImage);
    }
}

function submitPost(data){
    if(data.code==1){
        if(document.URL != data.href){
            if(document.URL != data.href){
                window.location = data.href;
            }
        }
    }
}

function getUpdatePostRow(row){
    var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
    html.find('#input-post-title').val(row.find('td a.input-post-title').html().trim());
    html.find('#input-post-slug').val(row.find('td a.input-post-slug').attr('data-slug').trim());
    html.find('#input-post-cate').val(row.find('td a.input-post-cate').attr('data-post-cate'));
    html.find('#input-post-order').val(row.find('td span.input-post-order').html().trim());
    html.find('#input-post-description').val(row.find('td.input-post-description').html());
    html.find('#input-post-publish-flg').prop('checked', row.find('td input.input-post-publish-flg').is(':checked'));
    html.find('#input-post-delete-flg').prop('checked', row.find('td input.input-post-delete-flg').is(':checked'));
    html.find('#input-post-id').val(row.attr('data-id'));
    return html;
}

function updatePostRow(data){
    var trEditting = $('tr.editting');
    var trTempUpdate = $('tr.tr-update-temp');
    trEditting.find('td a.input-post-title').html(trTempUpdate.find('#input-post-title').val());
    trEditting.find('td a.input-post-title').attr('href', data.href);
    trEditting.find('td a.input-post-slug').attr('href', data.front_href);
    trEditting.find('td a.input-post-slug').attr('data-slug', trTempUpdate.find('#input-post-slug').val().trim());
    trEditting.find('td a.input-post-cate label').html(trTempUpdate.find('#input-post-cate option:selected').text());
    trEditting.find('td a.input-post-cate').attr('data-post-cate' ,trTempUpdate.find('#input-post-cate').val());
    trEditting.find('td span.input-post-order').html(trTempUpdate.find('#input-post-order').val());
    trEditting.find('td.input-post-description').html(trTempUpdate.find('#input-post-description').val());
    trEditting.find('td input.input-post-publish-flg').prop('checked', trTempUpdate.find('#input-post-publish-flg').is(':checked'));
    trEditting.find('td input.input-post-delete-flg').prop('checked', trTempUpdate.find('#input-post-delete-flg').is(':checked'));
}

function changePostState(){
    $('input.input-post-publish-flg, input.input-post-delete-flg').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var post = {id: $(this).parents('tr').attr('data-id')};
        post[name] = value;
        changeState(url, {post: post});
    });
}

function evtAfterCropImage(data){
    var newImage = $('#post-temp-block').clone().removeAttr('id').removeClass('hidden');
    newImage.find('a.thumbnail').attr('data-name', data.file_name);
    newImage.find('a.thumbnail').attr('href', data.file_org_path);
    newImage.find('img.media-object').attr('src', data.file_path);
    $('ul#image-temp-upload').append(newImage);

    $('#input-crop-path').val('');
    $('.image-current').removeClass('hidden');
    $('.image-resize').addClass('hidden');
}

function setPostImage(){
    $(document.body).on('click', '.post-img-block', function(e) {
        e.preventDefault();
        var folder        = $(this).attr('data-folder');
        var file_name     = $(this).attr('data-name');
        var file_org_path = $(this).attr('href');
        var file_path     = $(this).find('img.media-object').attr('src');
        $('#input-post-img').val(folder + file_name);
        $('#post-img').attr('href', file_org_path);
        $('#post-img img.media-object').attr('src', file_path).removeAttr('style').removeAttr('data-src');
        $('#modal-change-img').modal('hide');
        return false;
    });
}